﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_QuanLyNhomHangHoa_ThemMoiNhomHangHoa : System.Web.UI.Page
{
    private DataAccess da = new DataAccess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminID"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập / You must login');location.href='/../Admin/Login.aspx';</script>");
        }
    }
    protected bool ExistsMaNhomHang(string strMa)
    {
        bool bOutPut = false;
        SqlDataReader drd = null;
        SqlCommand comm = new SqlCommand("isExistsMaNhomHang", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@ma", strMa);
        try
        {
            comm.Connection.Open();
            drd = comm.ExecuteReader();
            if (drd.Read())
                bOutPut = true;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
        finally
        {
            comm.Connection.Close();
            comm.Connection.Dispose();
        }
        return bOutPut;
    }
    protected void NhomHang_AddNew_Click(object sender, EventArgs e)
    {
        InsertNhomHang();
    }
    protected void InsertNhomHang()
    {

        string strMa = this.txtMa.Text;
        string strTenNhom = this.txtTenNhomHang.Text;
        if (ExistsMaNhomHang(strMa) != true)
        {
            SqlCommand comm = new SqlCommand("InsertNhomHang", da.SqlConn());
            comm.CommandType = CommandType.StoredProcedure;

            comm.Parameters.AddWithValue("@Ma", strMa);
            comm.Parameters.AddWithValue("@Ten", strTenNhom);

            comm.Connection.Open();
            if (comm.ExecuteNonQuery() == -1)
            {
                Response.Redirect("DanhSachNhomHangHoa.aspx");
            }

            comm.Connection.Close();
            comm.Connection.Dispose();
            Response.Redirect("DanhSachNhomHangHoa.aspx");

        }
        else
        {
            HttpContext.Current.Response.Write("<script language=\"javascript\">alert('Mã nhóm hàng này đã tồn tại !');</script>");
        }

    }
}
